package cn.niitcxl.controller;

import cn.niitcxl.services.OssService;
import cn.niitcxl.utils.R;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/uploadOss")
public class OssController {

    @Autowired
    private OssService ossService;

    @PostMapping
    public R uploadFileOss(MultipartFile file){
        String url =  ossService.uploadAvatar(file);

        return new R().ok().data("url",url);
    }

    /**
     * 对阿里云oss的文件进行删除
     * @param filePath
     * @return
     */
    @DeleteMapping
    public R deleteFileOss(@RequestParam("filePath") String filePath){
        if (Strings.isNotEmpty(filePath)){
            boolean isDelete =  ossService.deleteFile(filePath);
            if (isDelete){
                return new R().ok();
            }else {
                return new R().error().message("阿里云oss删除文件失败!");
            }

        }
     return new R().error().message("未选中上传文件,不用删除!");
    }
}